package com.lsh.controller.advices;

import com.lsh.service.IndexService;
import com.lsh.service.advices.AdviceCommentService;
import com.lsh.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

/**
 * 消息评价管理模块
 * @author Administrator
 * @date 2019/10/28 16:08
 */
@Controller
@RequestMapping("/user/advices")
public class AdviceCommentController {

    @Autowired
    IndexService indexService;
    @Autowired
    AdviceCommentService adviceCommentService;

    /**
     * 完成页面跳转，并获取当前用户名
     * @param token
     * @param mv
     * @return
     */
    @GetMapping("/comment")
    public ModelAndView showHtml(@CookieValue("TOKEN") String token, ModelAndView mv){
        String nickname = indexService.findNickname(token);
        String userId = indexService.findByUserId(token);
        mv.addObject("nickname", nickname);
        mv.setViewName("/user/advices/comment");
        return mv;
    }


    /**
     * 获取当前用户的未读评价记录数
     * @param token
     * @return
     */
    @GetMapping("/comment/unreadcount")
    @ResponseBody
    public Result getUnReadCount(@CookieValue("TOKEN") String token){
        String userId = indexService.findByUserId(token);
        int unCount = adviceCommentService.findUnReadCommentCount(userId);
        return new Result(1001, "", new HashMap<>()).addExtraData("unCount", unCount);
    }

    /**
     * 获取当前用户收到的所有评价消息
     * @param token
     * @return
     */
    @GetMapping("/comments/receive")
    @ResponseBody
    public Result getAdviceComment(@CookieValue("TOKEN") String token, @RequestParam("page") Integer pageNum){
        String userId = indexService.findByUserId(token);
        HashMap receiveComments = adviceCommentService.findReceiveCommentByUserId(userId, pageNum);
        return new Result("1001", "", receiveComments);
    }

    /**
     * 更新用户全部评价的未读情况
     * @param token
     * @return
     */
    @PutMapping("/comments/receive")
    @ResponseBody
    public Result updateAdviceCommentAll(@CookieValue("TOKEN") String token){
        String userId = indexService.findByUserId(token);
        adviceCommentService.updateIsReadAllByUserId(userId);
        return new Result(1001, "修改成功");
    }

    /**
     * 清空所有评价消息通知
     * @param token
     * @return
     */
    @DeleteMapping("/comments/receive")
    @ResponseBody
    public Result deleteAdviceCommentAll(@CookieValue("TOKEN") String token){
        String userId = indexService.findByUserId(token);
        adviceCommentService.deleteReceiveCommentAllByUserId(userId);
        return new Result(1001, "清空成功");
    }

    /**
     * 更新指定index的未读情况
     * @param token
     * @param index
     * @return
     */
    @PutMapping("/comment/receive")
    @ResponseBody
    public Result updateAdviceComment(@CookieValue("TOKEN") String token, @RequestParam("index") Integer index){
        String userId = indexService.findByUserId(token);
        adviceCommentService.updateIsReadByUserId(userId, index);
        return new Result("1001","修改成功");
    }


    /**
     * 删除指定index索引的消息
     * @param token
     * @param index
     * @return
     */
    @DeleteMapping("/comment/receive")
    @ResponseBody
    public Result deleteAdviceComment(@CookieValue("TOKEN") String token, @RequestParam("index") Integer index){
        String userId = indexService.findByUserId(token);
        adviceCommentService.deleteReceiveCommentByUserId(userId, index);
        return new Result(1001, "删除成功");
    }

}
